﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 对象数据
/// </summary>
public class ObjectInfo
{
	public GameObject obj;
	public TSprite sprite;
}

public class ObjectManager : MonoBehaviour {
	
	public enum TagEnum
	{
		Hero,
		Map,
		Monster,
		Bullet,
	}
	
	public Transform hero;
	public Transform map;
	public Transform monster;
	public Transform bullet;
	
	List<ObjectInfo> objectList = new List<ObjectInfo>();
	
	static ObjectManager instance;
	
	public static ObjectManager GetInstance()
	{
		return instance;
	}
	
	void Awake()
	{
		instance = this;
	}

//	// Update is called once per frame
//	void Update () {
//	
//	}
	
	public void OnUpdate()
	{
		for(int i = 0; i < ObjectInfoList.Count; i++)
		{
			if(ObjectInfoList[i].sprite != null)
				ObjectInfoList[i].sprite.OnUpdate();
		}
	}
	
	/// <summary>
	/// 设置model 数据
	/// </summary>
	public void ResetModel(GameObject go)
	{
		Transform parent = null;
		if(go.tag.Equals(TagEnum.Hero.ToString()))
		{
//			parent = hero;
			go.layer = LayerMask.NameToLayer("Sprite");
		}
		else if(go.tag.Equals(TagEnum.Map.ToString()))
		{
			parent = map;
			go.layer = LayerMask.NameToLayer(go.tag);
		}
		else if(go.tag.Equals(TagEnum.Monster.ToString()))
		{
			parent = monster;
			go.layer = LayerMask.NameToLayer("Sprite");
		}
		else if(go.tag.Equals(TagEnum.Bullet.ToString()))
		{
			parent = bullet;
			go.layer = LayerMask.NameToLayer("Sprite");
		}
		
		go.transform.parent = parent;
		
		if(!go.tag.Equals(TagEnum.Bullet.ToString()))
		{
			ObjectInfo info = new ObjectInfo();
			info.obj = go;
			info.sprite = go.GetComponent<TSprite>();
			ObjectInfoList.Add(info);
		}
		else
		{
			//如果是子弹的话, 特殊处理 放到 BulletManager 管理类里面
		}
		
	}
	
	/// <summary>
	/// 获取所有的地图对象
	/// </summary>
	public List<ObjectInfo> GetMapObjList()
	{
		List<ObjectInfo> list = new List<ObjectInfo>();
		foreach(ObjectInfo info in ObjectInfoList)
		{
			if(info.obj.tag.Equals(TagEnum.Map.ToString()))
				list.Add(info);
		}
		
		return list;
	}
	
	/// <summary>
	/// 获取所有的怪物对象
	/// </summary>
	public List<ObjectInfo> GetMonsterObjList()
	{
		List<ObjectInfo> list = new List<ObjectInfo>();
		foreach(ObjectInfo info in ObjectInfoList)
		{
			if(info.obj.tag.Equals(TagEnum.Monster.ToString()))
				list.Add(info);
		}
		
		return list;
	}
	
	/// <summary>
	/// 获取指定的 类型的对象
	/// </summary>
	public List<ObjectInfo> GetObjList(string type)
	{
		List<ObjectInfo> list = new List<ObjectInfo>();
		foreach(ObjectInfo info in ObjectInfoList)
		{
			if(info.obj.tag.Equals(type))
				list.Add(info);
		}
		
		return list;
	}
	
	/// <summary>
	/// 移除某一个  obj
	/// </summary>
	public void RemoveObj(GameObject go)
	{	
		for(int i = 0; i < ObjectInfoList.Count; i++)
		{
			if(ObjectInfoList[i].obj == go)
			{
				ObjectInfoList.RemoveAt(i);
				Destroy(go);
				return;
			}
		}
		
		Debug.LogError("对象列表中不包含此对象" + go);
		Destroy(go);
	}
	
	/// <summary>
	/// 清除所有对象
	/// </summary>
	public void Clear()
	{
		for(int i = 0; i < ObjectInfoList.Count; i++)
		{
			Destroy(ObjectInfoList[i].obj);
		}
		ObjectInfoList.Clear();
	}
	
	public List<ObjectInfo> ObjectInfoList
	{
		get{return objectList;}
	}
	
	public static List<T> FindInChildren<T>(GameObject go) where T : Component
	{
		return FindInChildren<T>(go, false);
	}
	
	static List<T> FindInChildren<T>(GameObject go, bool jsutOne) where T : Component
	{
		List<T> cps = new List<T>();
		
		if(go != null)
			FindInChildren<T>(go.transform, cps, jsutOne);
		
		return cps;
	}
	
	static void FindInChildren<T>(Transform trans, List<T> cps, bool justOne) where T : Component
	{
		T cp = trans.GetComponent<T>();
		if(cp != null)
			cps.Add(cp);
		
		if(justOne && cps.Count == 1)
			return;
		
		for(int i = 0; i < trans.childCount; i++)
		{
			FindInChildren<T>(trans.GetChild(i), cps, justOne);
			if(justOne && cps.Count == 1)
				break;
		}
	}
	
	public static T[] GetComponentsInChildren<T>(GameObject go) where T : Component
	{
		List<T> cps = FindInChildren<T>(go, false);
		return cps.ToArray();
	}
	
	public static T GetComponentInChildren<T>(GameObject go) where T : Component
	{
		List<T> cps = FindInChildren<T>(go, true);
		if(cps.Count < 1)
			return null;
		
		return cps[0];
	}
	
	static Camera mainCamera = null;
	public static Camera GetMainCamera()
	{
		if(mainCamera == null)
		{
			GameObject cameraObj = GameObject.FindWithTag("MainCamera");
			if(cameraObj != null)
				return cameraObj.GetComponent<Camera>();
		}
		else
		{
			return mainCamera;
		}
		
		return null;
	}
}
